package com.dgut.usermodule.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.usermodule.dao.UserJpaRepository;
import com.dgut.usermodule.entity.User;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	private UserJpaRepository dao;
	@Override
	public List<User> findAll() {
		return dao.findAll();
	}
	
	@Override
	public User findOne(Long id) {
		return dao.findOne(id);
	}

	@Override
	public User findByName(String name) {
		
		return dao.findByUserName(name);
	}

	@Override
	public String save(User user) {
		if (dao.findByUserName(user.getUserName())==null) {
			user.setDate(new Date());
			dao.save(user);
			return "true";
		}else {
			return "用户名重复！";
		}
	}

	@Override
	public String update(User user) {
		if (dao.save(user)!=null) {
			return "true";
		}
		return "false";
	}

	@Override
	public boolean delete(Long id) {
		if (dao.findOne(id)!=null) {
			dao.delete(id);
			return true;
		}
		return false;
	}

	@Override
	public Page<User> findPage(Specification<User> spec, Pageable pageable) {
		return dao.findAll(spec, pageable);
	}

}
